//
//  BaseTabBarViewController.m
//  jingdong
//
//  Created by 陈明光 on 2021/4/24.
//  Copyright © 2021 陈明光. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "ViewController.h"
#import "TLAnimationTabBar.h"
#import "BaseNavigationController.h"

#import "HomeViewController.h"
#import "CategoryViewController.h"
#import "FindViewController.h"
#import "CartViewController.h"
#import "MyViewController.h"
#import "HomeNavigationController.h"

@interface BaseTabBarViewController ()

@end

@implementation BaseTabBarViewController

//static BaseTabBarViewController *sharedBaseTabBar;

//+(BaseTabBarViewController *)sharedBaseTabBarViewController{
//    if (!sharedBaseTabBar) {
//        sharedBaseTabBar = [[BaseTabBarViewController alloc]init];
//        NSLog(@"初始化TabBar");
//    }
//    return sharedBaseTabBar;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *titleArray = @[@"首页",@"分类",@"发现",@"购物车",@"我的"];
    //for (int i = 0; i<titleArray.count; i++) {
        
    
    [self addChildViewController:childViewController(titleArray[0], @"tabBar_home_normal", 0)];
    [self addChildViewController:childViewController(@"分类", @"user_00084", 1)];
    [self addChildViewController:childViewController(@"发现",@"tabBar_find_normal", 2)];
    [self addChildViewController:childViewController(@"购物车", @"drop", 3)];
    [self addChildViewController:childViewController(@"我的", @"Tools_00028", 4)];
    //}
    self.tabBar.tintColor = [UIColor colorWithRed:234/255.0 green:111/255.0 blue:90/255.0 alpha:1];
    if (@available(iOS 10.0, *)) {
        self.tabBar.unselectedItemTintColor = [UIColor colorWithRed:124/255.0 green:99/255.0 blue:86/255.0 alpha:1];
    } else {
        // Fallback on earlier versions
    }
}

- (void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title{
     childVc.title = title;
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:childVc];
    [self addChildViewController:nav];
}

// MARK: - UITabBarItem创建函数
/// 自定义样式UITabBarItem
UIViewController *childViewController (NSString *title, NSString *imgName, NSUInteger tag) {
    
    if (tag == 0) {
        HomeViewController *vc = [[HomeViewController alloc] init];
        vc.title = @"首页";
        HomeNavigationController *nav = [[HomeNavigationController alloc]initWithRootViewController:vc];
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgName] tag:tag];
        setAnimation(nav.tabBarItem, tag);
        return nav;
    }else if (tag == 1){
        CategoryViewController *vc = [[CategoryViewController alloc] init];
        vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgName] tag:tag];
         BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:vc];
        setAnimation(nav.tabBarItem, tag);
        return nav;
    }else if (tag == 2){
        FindViewController *vc = [[FindViewController alloc] init];
        
        BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:vc];
        
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgName] tag:tag];
        
        setAnimation(nav.tabBarItem, tag);
        return nav;
    }else if (tag == 3){
        CartViewController *vc = [[CartViewController alloc] init];
        vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgName] tag:tag];
        
        setAnimation(vc.tabBarItem, tag);
        return vc;
    }else{
        MyViewController *vc = [[MyViewController alloc] init];
        vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imgName] tag:tag];
         BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:vc];
        setAnimation(nav.tabBarItem, tag);
        return nav;
    }
    
   
}

/// 系统样式UITabBarItem
UIViewController *childViewController2 (UITabBarSystemItem systemItem, NSUInteger tag) {
    FindViewController *vc = [[FindViewController alloc] init];
    vc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:tag];
    
    setAnimation(vc.tabBarItem, tag);
    return vc;
}

// MARK: - 给UITabBarItem绑定动画
/// 给UITabBarItem绑定动画
void setAnimation(UITabBarItem *item, NSInteger index) {
    item.animation = @[
        bounceAnimation(), rotationAnimation(), transitionAniamtion(),
        fumeAnimation(), frameAnimation()
    ][index];
}

// MARK: - 创建动画函数
TLBounceAnimation *bounceAnimation(){
    TLBounceAnimation *anm = [TLBounceAnimation new];
    anm.isPlayFireworksAnimation = YES;
    return anm;
}

TLRotationAnimation *rotationAnimation(){
    TLRotationAnimation *anm = [TLRotationAnimation new];
    return anm;
}

TLTransitionAniamtion *transitionAniamtion(){
    TLTransitionAniamtion *anm = [TLTransitionAniamtion new];
    anm.direction = 1; // 1~6
    anm.disableDeselectAnimation = NO;
    return anm;
}

TLFumeAnimation *fumeAnimation(){
    TLFumeAnimation *anm = [TLFumeAnimation new];
    return anm;
}

TLFrameAnimation *frameAnimation(){
    TLFrameAnimation *anm = [TLFrameAnimation new];
    anm.images = imgs();
    anm.isPlayFireworksAnimation = YES;
    return anm;
}

NSArray *imgs (){
    NSMutableArray *temp = [NSMutableArray array];
    for (NSInteger i = 28 ; i <= 65; i++) {
        NSString *imgName = [NSString stringWithFormat:@"Tools_000%zi", i];
        CGImageRef img = [UIImage imageNamed:imgName].CGImage;
        [temp addObject:(__bridge id _Nonnull)(img)];
    }
    return temp;
}

// MARK: - UITabBarItemDelegate 监听TabBarItem点击事件
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    // TabBarItem被点击时会被调用
    NSLog(@"%s",__func__);
}



@end
